<?php

namespace App\Http\Controllers;

use App\Http\Requests\RegisterationForm;
use App\User;
use App\UserActivation;
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;



class RegistrationController extends Controller
{
    public function create(){
        return view('registration.create');
    }

    public function store(RegisterationForm $request){
        /*
        $this->validate(\request(),[
            'name'=>'required',
            'email'=>'required|email',
            'password'=>'required|confirmed',
        ]);



        $user = User::create(\request(['name','password','email']));

        auth()->login($user);

        \Mail::to($user)->send(new Welcome);
        */

        $request->persist();

        return redirect()->home();
    }

    public function send(){
        $token = bcrypt(auth()->user()->email.time());
        $user = auth()->user();

        Mail::send('email.activation',compact('user','token'),function ($message){
            $from = ['From: aoswce@126.com','aoswce'];
            $to = ['To: '.auth()->user()->email,auth()->user()->name];
            $subject = 'blog  Activation!';
            $message->to($to)
                ->from($from)
                ->subject($subject);
        });

        $user->addActivationsData($token);

        return redirect('/');
    }

    public function active(){
        $token = request('verify');
        $rs = UserActivation::where('token',$token)
            ->notExpired();


        if($rs->exists()){
            $rs->update(['active'=>true]);
            return redirect('/login');
        }

        return redirect('/');
    }
}


